// SPDX-FileCopyrightText: 2023-2025 Sangfor Technologies Inc.
// SPDX-License-Identifier: Mulan PSL v2
package config

import (
	"io/ioutil"

	"gopkg.in/yaml.v2"

	"code.geniusai.org/notebook-manager/pkg/log"
)

const (
	AESKey1Path    = "/matrix/etc/conf/152613e1a9c8ceff/7e267b8d2db6c769"
	AESKey2Path    = "/matrix/etc/conf/19885c245bcba35e/3f3f3159880cf076"
	AESKey3        = "a9c5d7f6bca306fac4744e1a9c5d7ffa"
	AESKeySaltPath = "/matrix/etc/conf/7c1829ad/71939c51"
)

type Conf struct {
	SwaggerUI   bool            `yaml:"swagger_ui"`
	Mysql       MysqlConf       `yaml:"mysql"`
	Prometheus  PrometheusConf  `yaml:"prometheus"`
	OrionUi     OrionUiConf     `yaml:"orion-root-gui"`
	StatPvc     StatPvcConf     `yaml:"stat-pvc-app"`
	IstioAuth   StatPvcConf     `yaml:"istio-auth"`
	TrainManger TrainMangerConf `yaml:"train-manager"`
	DataReport  DataReportConf  `yaml:"data-report"`
}

type PrometheusConf struct {
	Url  string `yaml:"url"`
	Port string `yaml:"port"`
}

type OrionUiConf struct {
	Url  string `yaml:"url"`
	Port string `yaml:"port"`
}

type StatPvcConf struct {
	Url  string `yaml:"url"`
	Port string `yaml:"port"`
}

type IstioAuthConf struct {
	Url  string `yaml:"url"`
	Port string `yaml:"port"`
}

type TrainMangerConf struct {
	Url  string `yaml:"url"`
	Port string `yaml:"port"`
}

type MysqlConf struct {
	User   string `yaml:"user"`
	Passwd string `yaml:"passwd"`
	Url    string `yaml:"url"`
	Port   string `yaml:"port"`
	DbName string `yaml:"db_name"`
	Args   string `yaml:"args"`

	LogLevel string `yaml:"log_level"`
}

type DataReportConf struct {
	PulsarServiceUrl string `yaml:"pulsar-service-url"`
	NotebookTopic    string `yaml:"notebook-topic"`
	EnvType          string `yaml:"env-type"`
}

func LoadConf(conf string) *Conf {
	confData := Conf{}
	if data, err := ioutil.ReadFile(conf); err != nil {
		log.Panicf("cant find conf file: %s", conf)
	} else {
		if err = yaml.Unmarshal(data, &confData); err != nil {
			log.Panicf("cant load conf file: %s", conf)
		}
	}
	return &confData
}
